// test_read_trade_shm.cpp
#include "shm_utils.h"
#include <iostream>
#include <thread>
#include <chrono>
#include <csignal>

int main() {
    try {
        auto* shm_ptr = link_shm_read_only<MARKETStruct>("_v1");
        if (!shm_ptr) {
            std::cerr << "❌ 无法连接共享内存 _v1" << std::endl;
            return 1;
        }

        size_t last_index = 0;
        std::cout << "🚀 启动成交共享内存读取程序" << std::endl;

        while (true) {
            size_t write_index = shm_ptr->tradeShm.next_write.load();

            while (last_index < write_index) {
                const auto& trade = shm_ptr->tradeShm[last_index];
                std::cout << "💹 成交:"
                          << " market=" << trade.market_id
                          << " trade_id=" << trade.trade_id
                          << " price=" << trade.price
                          << " size=" << trade.size
                          << " bid_id=" << trade.bid_id
                          << " ask_id=" << trade.ask_id
                          << " ts=" << trade.timestamp
                          << std::endl;

                ++last_index;
            }

            std::this_thread::sleep_for(std::chrono::milliseconds(100));
        }

    } catch (const std::exception& e) {
        std::cerr << "❌ Error: " << e.what() << std::endl;
    }
}
